package com.example.demo.common.response;

import com.github.pagehelper.PageInfo;
import lombok.Data;

import java.io.Serializable;
import java.util.List;

/**
 *
 * 通用分页查询返回类型
 */
@Data
public class ResponsePage<T> implements Serializable {

    private Integer pageNum;
    private Integer pageSize;
    private Long totalCount;
    private Integer totalPages;
    private List<T> list;

    // 下面定义一个方法,实现将PageInfo类型中的对象的值赋值给当前ResponsePage对象对应的属性
    // 相当于一个转换方法
    public static <T> ResponsePage<T> restPage(PageInfo<T> pageInfo){
        ResponsePage<T> result=new ResponsePage<>();
        result.setPageNum(pageInfo.getPageNum());
        result.setPageSize(pageInfo.getPageSize());
        result.setTotalCount(pageInfo.getTotal());
        result.setTotalPages(pageInfo.getPages());
        // 将pageInfo中的查询结果结合也要赋值过去
        result.setList(pageInfo.getList());
        return  result;

    }

    /**
     * 将PageHelper分页后的list转为分页信息
     */
    public static <T> ResponsePage<T> restPage(List<T> list) {
        ResponsePage<T> result = new ResponsePage<T>();
        PageInfo<T> pageInfo = new PageInfo<T>(list);
        result.setTotalPages(pageInfo.getPages());
        result.setPageNum(pageInfo.getPageNum());
        result.setPageSize(pageInfo.getPageSize());
        result.setTotalCount(pageInfo.getTotal());
        result.setList(pageInfo.getList());
        return result;
    }

}
